package com.info33.platform.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.dto.SysAppMenuRoleDTO;
import com.info33.platform.system.dto.SysPostDTO;
import com.info33.platform.system.dto.SysUserDTO;
import com.info33.platform.system.entity.SysAppMenuRole;
import com.info33.platform.system.service.SysAppMenuRoleService;
import com.info33.platform.system.vo.SysAppMenuVO;
import com.info33.platform.system.vo.SysPostVO;
import com.info33.platform.system.vo.SysTenantUserVO;
import com.info33.platform.system.vo.SysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author admin
 */
@RestController
@RequestMapping("/SysAppMenuRole")
@Api(tags = "系统 - 管理端 app - 菜单 - 用户/角色/全部 关联")
public class SysAppMenuRoleController extends BaseController<SysAppMenuRoleService, SysAppMenuRole> {

    private final SysAppMenuRoleService sysAppMenuRoleService;

    @Autowired
    public SysAppMenuRoleController(SysAppMenuRoleService sysAppMenuRoleService) {
        super(sysAppMenuRoleService);
        this.sysAppMenuRoleService = sysAppMenuRoleService;
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * save
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @PostMapping(value = "/bind/role", name = "只是新增，单角色新增")
    @ApiOperation(value = "pc - 菜单绑定角色")
    public Result<Object> pcBindRole(
            @RequestBody SysAppMenuRoleDTO dto) {
        dto.setTenantId(getTenantId());
        return sysAppMenuRoleService.pcBindRole(dto);
    }

    @PostMapping(value = "/bind/user")
    @ApiOperation(value = "pc - 菜单绑定用户", notes = "只是新增，多用户新增")
    public Result<Object> pcBindUser(
            @RequestBody SysAppMenuRoleDTO dto) {
        dto.setTenantId(getTenantId());
        return sysAppMenuRoleService.pcBindUser(dto);
    }

    @PostMapping(value = "/bind/all")
    @ApiOperation(value = "pc - 菜单绑定全部", notes = "只是新增，仅需要传入菜单 id 集合")
    public Result<Object> pcBindAll(
            @RequestBody SysAppMenuRoleDTO dto) {
        dto.setTenantId(getTenantId());
        return sysAppMenuRoleService.pcBindAll(dto);
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * list（全部菜单没有 list api）
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @PostMapping(value = "/user/{pageNum}/{pageSize}")
    @ApiOperation(value = "pc - 拥有权限的用户", notes = "入参说明<br/>" +
            "trueName：模糊查询<br/>" +
            "mobile：模糊查询")
    public Result<IPage<SysUserVO>> pcUser(
            @PathVariable int pageNum,
            @PathVariable int pageSize,
            @RequestBody SysUserDTO dto) {
        dto.setTenantId(getTenantId());
        return Result.ok(sysAppMenuRoleService.pcUser(new Page<>(pageNum, pageSize), dto));
    }

    @PostMapping(value = "/role/{pageNum}/{pageSize}")
    @ApiOperation(value = "pc - 拥有权限的角色", notes = "入参说明<br/>" +
            "name：模糊查询")
    public Result<IPage<SysPostVO>> pcRole(
            @PathVariable int pageNum,
            @PathVariable int pageSize,
            @RequestBody SysPostDTO dto) {
        dto.setTenantId(getTenantId());
        return Result.ok(sysAppMenuRoleService.pcRole(new Page<>(pageNum, pageSize), dto));
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * detail
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @GetMapping(value = "/user/{uid}")
    @ApiOperation(value = "pc - 拥有权限的某用户详情", notes = "R 租户用户 id")
    public Result<SysTenantUserVO> pcUserDetail(
            @PathVariable(value = "uid") Long uid) {
        return Result.ok(sysAppMenuRoleService.pcUserDetail(uid));
    }

    @GetMapping(value = "/role/{rid}")
    @ApiOperation(value = "pc - 拥有权限的某角色详情", notes = "R 查询租户角色 id")
    public Result<SysPostVO> pcRoleDetail(
            @PathVariable(value = "rid") Long rid) {
        return Result.ok(sysAppMenuRoleService.pcRoleDetail(rid));
    }

    @GetMapping(value = "/all")
    @ApiOperation(value = "pc - 绑定全部的菜单 id 集合", notes = "R 查询")
    public Result<List<SysAppMenuVO>> pcAll() {
        return Result.ok(sysAppMenuRoleService.pcAll(getTenantId()));
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * update
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @PutMapping(value = "/edit/user")
    @ApiOperation(value = "pc - 编辑用户")
    public Result<Object> pcEditUser(
            @RequestBody SysAppMenuRoleDTO dto) {
        return sysAppMenuRoleService.pcEditUser(dto);
    }

    @PutMapping(value = "/edit/role")
    @ApiOperation(value = "pc - 编辑角色")
    public Result<Object> pcEditRole(
            @RequestBody SysAppMenuRoleDTO dto) {
        return sysAppMenuRoleService.pcEditRole(dto);
    }

    @PutMapping(value = "/edit/all")
    @ApiOperation(value = "pc - 编辑全部")
    public Result<Object> pcEditAll(
            @RequestBody SysAppMenuRoleDTO dto) {
        dto.setTenantId(getTenantId());
        return sysAppMenuRoleService.pcEditAll(dto);
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * remove from db
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @DeleteMapping(value = "/remove/user/{uid}")
    @ApiOperation(value = "pc - 删除某用户所有菜单", notes = "后期优化：避免全盘扫描，根据 id 删除")
    public Result<Object> pcRemoveUser(
            @PathVariable(value = "uid") Long uid) {
        return sysAppMenuRoleService.remove(new QueryWrapper<SysAppMenuRole>()
                .eq("user_id", uid)
                .eq("type", 2))
                ? Result.ok() : Result.error();
    }

    @DeleteMapping(value = "/remove/role/{rid}")
    @ApiOperation(value = "pc - 删除某角色所有菜单", notes = "后期优化：避免全盘扫描，根据 id 删除")
    public Result<Object> pcRemoveRole(
            @PathVariable(value = "rid") Long rid) {
        return sysAppMenuRoleService.remove(new QueryWrapper<SysAppMenuRole>()
                .eq("role_id", rid)
                .eq("type", 1))
                ? Result.ok() : Result.error();
    }

    @DeleteMapping(value = "/remove/all")
    @ApiOperation(value = "pc - 删除所有政权为全部的菜单", notes = "后期优化：避免全盘扫描，根据 id 删除")
    public Result<Object> pcRemoveAll() {
        return sysAppMenuRoleService.remove(new QueryWrapper<SysAppMenuRole>()
                .eq("type", 3)
                .eq(TableConstant.TENANT_ID, getTenantId()))
                ? Result.ok() : Result.error();
    }


}